<?php
/**
 * 作者：New一个
 * 主页：http://www.newyige.com
 * 描述：WordPress优化
 * 时间：2016.05.10
 */

//~ 禁用Google Open Sans字体
add_filter('gettext_with_context', 'disable_open_sans', 888, 4);
function disable_open_sans($translations, $text, $context, $domain) {
	if ('Open Sans font: on or off' == $context && 'on' == $text)
		$translations = 'off';
	return $translations;
}

//~ 更改后台字体
add_action('admin_head', 'admin_lettering');
function admin_lettering() {
	echo '<style type="text/css">
        * { font-family: "Microsoft YaHei" }
		#wpadminbar * { font-family: "Microsoft YaHei" }
		#latest-comments img{ width: 52px; height: 52px; }
        </style>';
}

//~ 官方Gravatar头像调用ssl头像链接
//add_filter('get_avatar', 'get_ssl_avatar');
//function get_ssl_avatar($avatar) {
//	$avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/', '<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">', $avatar);
//	return $avatar;
//}
//~ 调用多说头像链接
//add_filter('get_avatar', 'mytheme_get_avatar');
//function mytheme_get_avatar( $avatar ) {
//$avatar = preg_replace( "/http:\/\/(www|\d).gravatar.com/","http://gravatar.duoshuo.com",$avatar );
//return $avatar;
//}
add_filter('get_avatar', 'my_custom_avatar', 1, 5);
function my_custom_avatar($avatar, $id_or_email, $size, $default, $alt) {

	if (!empty($id_or_email -> user_id)) {
		$avatar = get_template_directory_uri() . '/images/avatar.jpg';
	} else {
		$avatar = get_template_directory_uri() . '/images/avatar.jpg';
	}
	$avatar = '<img alt="' . $alt . '" src=' . $avatar . ' class="avatar avatar-' . $size . '" width=' . $size . ' height=' . $size . ' />';

	return $avatar;
}

//~ 编辑器增强
add_filter("mce_buttons_4", "add_editor_buttons");
function add_editor_buttons($buttons) {
	$buttons[] = 'fontselect';
	$buttons[] = 'fontsizeselect';
	$buttons[] = 'styleselect';
	$buttons[] = 'backcolor';
	$buttons[] = 'hr';
	$buttons[] = 'wp_page';
	$buttons[] = 'copy';
	$buttons[] = 'paste';
	return $buttons;
}

//~ 自动重命名图片和附件避免重复或因中文乱码
add_filter('wp_handle_upload_prefilter', 'file_rename');
function file_rename($file) {
	$time = date("Y-m-d");
	$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
	return $file;
}

//~ 禁用文件类型检测
define('ALLOW_UNFILTERED_UPLOADS', true);

//~ 隐藏admin Bar
add_filter('show_admin_bar', '__return_false');

//~ 禁止自动转换符号
add_filter('run_wptexturize', '__return_false');

//~ 移除Emoji表情
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');

//~ 移除head标签内多余代码
if (!is_admin()) {
	remove_action('wp_head', 'feed_links', 2);
	remove_action('wp_head', 'feed_links_extra', 3);
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'index_rel_link');
	remove_action('wp_head', 'parent_post_rel_link', 10, 0);
	remove_action('wp_head', 'start_post_rel_link', 10, 0);
	remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
	remove_action('wp_head', 'locale_stylesheet');
	remove_action('publish_future_post', 'check_and_publish_future_post', 10, 1);
	remove_action('wp_head', 'noindex', 1);
	remove_action('wp_head', 'wp_generator');
	remove_action('wp_head', 'rel_canonical');
	remove_action('wp_footer', 'wp_print_footer_scripts');
	remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
	remove_action('template_redirect', 'wp_shortlink_header', 11, 0);

	function my_remove_recent_comments_style() {
		global $wp_widget_factory;
		remove_action('wp_head', array($wp_widget_factory -> widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
	}

}

//~ 移除 WordPress 加载的JS和CSS链接中的版本号
add_filter('style_loader_src', 'remove_cssjs_ver', 999);
add_filter('script_loader_src', 'remove_cssjs_ver', 999);
function remove_cssjs_ver($src) {
	if (strpos($src, 'ver='))
		$src = remove_query_arg('ver', $src);
	return $src;
}

//~ 屏蔽默认小工具
add_action('widgets_init', 'my_unregister_widgets');
function my_unregister_widgets() {
	unregister_widget('WP_Widget_Archives');
	unregister_widget('WP_Widget_Calendar');
	unregister_widget('WP_Widget_Categories');
	unregister_widget('WP_Widget_Links');
	unregister_widget('WP_Widget_Meta');
	unregister_widget('WP_Widget_Pages');
	unregister_widget('WP_Widget_Recent_Comments');
	unregister_widget('WP_Widget_Recent_Posts');
	unregister_widget('WP_Widget_RSS');
	unregister_widget('WP_Widget_Search');
	unregister_widget('WP_Widget_Tag_Cloud');
	unregister_widget('WP_Widget_Text');
	unregister_widget('WP_Nav_Menu_Widget');
}
?>